<template>
  <view class="login-container">
    <view class="login-box">
      <view class="login-header">
        <text class="login-title">管理员登录</text>
        <text class="login-subtitle">请使用管理员账号登录系统</text>
      </view>
      
      <view class="login-form">
        <view class="form-item">
          <text class="label">账号</text>
          <input 
            class="input" 
            type="text" 
            placeholder="请输入管理员账号"
            v-model="formData.account"
            @input="onAccountInput"
          />
        </view>
        
        <view class="form-item">
          <text class="label">密码</text>
          <input 
            class="input" 
            type="password" 
            placeholder="请输入密码"
            v-model="formData.password"
            @input="onPasswordInput"
          />
        </view>
        
        <button 
          class="login-btn" 
          :disabled="!canLogin"
          @click="handleLogin"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </view>
      
      <view class="login-tips">
        <text class="tips-text">* 此登录功能仅支持Web端访问</text>
        <text class="tips-text">* 学生请使用微信小程序进行报道</text>
        <text class="tips-text">* 演示账号：admin / 123456</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import platformGuard from '@/utils/platform-guard.js';

// 表单数据
const formData = ref({
  account: '',
  password: ''
});

// 加载状态
const loading = ref(false);

// 计算是否可以登录
const canLogin = computed(() => {
  return formData.value.account.trim() && formData.value.password.trim() && !loading.value;
});

// 账号输入
const onAccountInput = (e) => {
  formData.value.account = e.detail.value;
};

// 密码输入
const onPasswordInput = (e) => {
  formData.value.password = e.detail.value;
};

// 处理登录
const handleLogin = async () => {
  if (!canLogin.value) return;
  
  loading.value = true;
  
  try {
    // 模拟登录验证
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 简单的账号密码验证（实际项目中应该调用后端接口）
    if (formData.value.account === 'admin' && formData.value.password === '123456') {
      // 登录成功，保存token和用户信息
      const mockToken = 'mock_token_' + Date.now();
      const mockUserInfo = {
        id: 1,
        account: 'admin',
        name: '管理员',
        role: ['counselor'] // 可以根据需要修改角色
      };
      
      uni.setStorageSync('token', mockToken);
      uni.setStorageSync('userInfo', mockUserInfo);
      
      uni.showToast({
        title: '登录成功',
        icon: 'success'
      });
      
      // 跳转到管理页面
      setTimeout(() => {
        platformGuard.reLaunch('/pages/admin/index');
      }, 1500);
      
    } else {
      // 登录失败
      uni.showToast({
        title: '账号或密码错误',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('登录失败:', error);
    uni.showToast({
      title: '登录失败，请重试',
      icon: 'none'
    });
  } finally {
    loading.value = false;
  }
};

// 页面加载时检查平台
onMounted(() => {
  // 检查是否在Web端
  if (platformGuard.platform !== 'web') {
    uni.showModal({
      title: '访问受限',
      content: '此页面仅支持Web端访问，请使用浏览器打开',
      showCancel: false,
      success: () => {
        // 小程序端跳转到学生首页
        if (platformGuard.platform === 'miniprogram') {
          platformGuard.reLaunch('/pages/dashboard/index');
        }
      }
    });
    return;
  }
  
  // Web端检查是否已登录
  const token = uni.getStorageSync('token');
  if (token) {
    // 已登录，直接跳转到管理端首页
    platformGuard.reLaunch('/pages/admin/index');
  }
});
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
}

.login-box {
  width: 100%;
  max-width: 600rpx;
  background: #fff;
  border-radius: 20rpx;
  padding: 60rpx 40rpx;
  box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 60rpx;
}

.login-title {
  font-size: 48rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 20rpx;
}

.login-subtitle {
  font-size: 28rpx;
  color: #666;
  display: block;
}

.login-form {
  margin-bottom: 40rpx;
}

.form-item {
  margin-bottom: 40rpx;
}

.label {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.input {
  width: 100%;
  height: 80rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 10rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  background: #f8f9fa;
  box-sizing: border-box;
}

.input:focus {
  border-color: #667eea;
  background: #fff;
}

.login-btn {
  width: 100%;
  height: 80rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 10rpx;
  font-size: 32rpx;
  font-weight: bold;
  margin-top: 20rpx;
}

.login-btn:disabled {
  background: #ccc;
  color: #999;
}

.login-tips {
  text-align: center;
}

.tips-text {
  font-size: 24rpx;
  color: #999;
  display: block;
  margin-bottom: 10rpx;
}
</style> 